package org.sun.sunshine.io.bio;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

//BIO 的案例
public class BioServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket=new ServerSocket(9999);
        while(true){
            //可以启动多个线程，每个线程处理一个客户端连接
            //new Thread(new BioServerHandler(serverSocket.accept())).start();
            //如果下面的处理没完成就会一直阻塞在这里。
            Socket socket=serverSocket.accept();
            InputStream inputStream=socket.getInputStream();
            byte[] bytes=new byte[1024];
            int len=0;
            while((len=inputStream.read(bytes))!=-1){
                System.out.println(new String(bytes,0,len));
            }
            //此时等待一会
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("end");
            inputStream.close();
        }
    }
}
